package com.kuang;

/**
 * ClassName: DeadLock
 * Description:
 * Date: 2021/3/16 20:47
 * Company: TravelSky
 *
 * @author YangXin
 */
public class DeadLock {
    //死锁:多个线程互相抱着对方需要的资源,然后形成僵持
    public static void main(String[] args) {
        Makeup makeup1 = new Makeup(0,"aaa");
        Makeup makeup2 = new Makeup(1,"bbb");
        makeup1.start();
        makeup2.start();
    }
}
class Mirror{
}
class Lipstick{
}
class Makeup extends Thread{
    static Lipstick lipstick = new Lipstick();
    static Mirror mirror = new Mirror();
    int choice;
    String girlName;

    Makeup(int choice,String girlName){
        this.choice = choice;
        this.girlName = girlName;
    }
    @Override
    public void run() {
        try {
            makeup();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    private void makeup() throws InterruptedException {
        if (choice == 0){
            synchronized (lipstick){
                System.out.println(this.girlName+"获得lipstick的锁...");
                Thread.sleep(1000);
                synchronized (mirror){
                    System.out.println(this.girlName+"获得mirror的锁...");
                }
            }
        }else {
            synchronized (mirror){
                System.out.println(this.girlName+"获得mirror的锁...");
                Thread.sleep(1000);
                synchronized (lipstick){
                    System.out.println(this.girlName+"获得lipstick的锁...");
                }
            }
        }
    }
}